<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/bootstrap.min.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/jedate.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/fonts/iconfont.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/common.css"/>
		<link rel="stylesheet" type="text/css" href="../../../../resources/layui/css/layui.css"/>

		<style>
			.deteleButton{display: block;}
			.diy .addButton{display: block!important;}
			.diy .deteleButton{display: none!important;}
			.addButton{display: none;}
		</style>
	</head>
	<body>
		<div class="container-fluid">
			<div class="row mheader">
				<div class="col-xs-2 text-left"> <a href="javascript:;" onclick="javascript:history.back(-1);"><span class="iconfont icon-fanhui"></span></a> </div>
				<div class="col-xs-8"><h2>申请材料</h2></div>
				<div class="col-xs-2 text-right" onclick="tj()"><a href="#">确定</span></a></div>
			</div>
			<div class="row">
				<div class="com-form ">
					<form class="form-horizontal">
						<div class="form-group">
					    	<label class="col-xs-3 control-label" for="">项目名称:</label>
					    	<div class="col-xs-9">
					    		<div class="select-box">
						    		<span class="caret"></span>
									<select class="form-control" id="proName" name="cc_UUID">
									</select>
								</div>
							</div>
						</div>
						<div class="form-group">
					    	<label class="col-xs-3 control-label" for="">供货时间:</label>
					    	<div class="col-xs-9">
								<input type="text" class="layui-input" id="gtime" name="supply_Time" placeholder="请输入供货时间">
					    	</div>	
					  	</div>
					  	<div class="form-group">
					    	<label class="col-xs-3 control-label" for="">申请人:</label>
					    	<div class="col-xs-9">
					    		<input type="text" class="form-control" id="sqr" placeholder="" value="" name="feed_sqr">
					  		</div>
					    </div>
						<div class="form-group">
							<label class="col-xs-3 control-label" for="">收货地址及联系方式:</label>
							<div class="col-xs-9">
								<input type="text" class="form-control" id="" placeholder="请您务必输入收货地址及联系方式" value="" name="f_adr">
							</div>
						</div>
					    <div class="addMaterial diy">
					    	<a href="javascript:;" class="mBtn addButton"><i
									class="iconfont icon-jia1"></i></a>
							<a href="javascript:;" class="mBtn deteleButton"><i class="iconfont icon-jian"></i></a>
							<div class="divider"></div>
						 	<div class="form-group">
						    	<label class="col-xs-3 control-label" for="">材料名:</label>
						    	<div class="col-xs-9">
						    		<div class="select-box">
							    		<span class="caret"></span>
										<select class="form-control" onchange="loadBit(this)" class="mat_id">

										</select>
									</div>
								</div>
							</div>
						  	<div class="form-group">
						    	<label class="col-xs-3 control-label" for="">申请数量:</label>
						    	<div class="col-xs-9">
						    		<input type="text" class="form-control" placeholder="" value="0" name="fnum">
						    	</div>
						  	</div>
							<div class="form-group">
								<label class="col-xs-3 control-label" for="">计量单位:</label>
								<div class="col-xs-9">
									<input type="text" readonly class="form-control" id="mat_bit" placeholder="" value="">
								</div>
							</div>
					  </div>
						<!--<div class="addMaterial">-->
					    	<!--<a href="javascript:;" class="mBtn deteleButton"><i class="iconfont icon-jian"></i></a>-->
						    <!--<div class="divider"></div>-->
						 	<!--<div class="form-group">-->
						    	<!--<label class="col-xs-3 control-label" for="">材料名:</label>-->
						    	<!--<div class="col-xs-9">-->
						    		<!--<div class="select-box">-->
							    		<!--<span class="caret"></span>-->
										<!--<select class="form-control" onchange="loadBit(this)" >-->

										<!--</select>-->
									<!--</div>-->
								<!--</div>-->
							<!--</div>-->
							<!--<div class="form-group">-->
								<!--<label class="col-xs-3 control-label" for="">申请数量:</label>-->
								<!--<div class="col-xs-9">-->
									<!--<input type="text" class="form-control"  placeholder="" value="0" name="fnum">-->
								<!--</div>-->
							<!--</div>-->
							<!--<div class="form-group">-->
								<!--<label class="col-xs-3 control-label" for="">计量单位:</label>-->
								<!--<div class="col-xs-9">-->
									<!--<input type="text" readonly class="form-control" placeholder="" value="">-->
								<!--</div>-->
							<!--</div>-->
					  <!--</div>-->
					</form>
				</div>
			</div>
			
		</div>
	</body>
</html>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src=".../../../resource/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../resource/js/jquery.jedate.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../resource/js/style.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../resources/js/template-web.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../resources/layui/layui.js"></script>

<script>
    layui.use('laydate', function() {
        var laydate = layui.laydate;

        laydate.render({
            elem: '#gtime'
            ,type: 'datetime'
        });
    })


	$(document).ready(function() {
	    //获取合同下拉
        ajaxs('api/public/loadPriDropDown.do',
            'post',
            {'type':'d','p':getP()},
            function (data) {
            	var html = '';
            	$.each(data.data,function (i,item) {
            	    //合同下拉
                    html += '<option value="'+item.cc_UUID+'">'+item.proName+'</option> ';
                });
				$("#proName").html(html);
			}
		);
        //获取材料下拉
        ajaxs('api/public/loadPriDropDown.do',
            'post',
            {'type':'b','p':getP()},
            function (data) {
            console.log(data);
                var html = '<option value="">---请选择---</option>';
                $.each(data.data,function (i,item) {
                    //合同下拉
                    html +=
						'<option value="'+item.mat_id+'" data-bit="'+item.mat_Bit+'" mat-type="'+item.mat_Type+'">'+item.mat_Name+'</option> ';
                });
                $(".addMaterial").each(function () {
					$(this).find(".select-box .form-control").html(html);

                })
                _htmls=$('.addMaterial').html();

            }
        );
        var strs= new Array(); //定义一数组
        strs = getP().split("|");
		$("#sqr").val(strs[0]);
		//YYYY-MM-DD hh:mm:ss

	});
	/**
     *  加载单位
     * @param obj  select对象
     */
    function loadBit(obj){
        //取选中项单位
        var bit = obj.options[obj.options.selectedIndex].dataset.bit;
        var mat=$(obj).find("option:selected").attr("mat-type");
		$(obj).parents('.addMaterial').children().next().next().next().next().children('.col-xs-9').children('input').attr('mat',mat);


        //赋值单位
		$(obj).parent().parent().parent().parent().find("#mat_bit").val(bit);
    }
	/**
	 *  提交
     */
    var p=true;
    var _htmls;

    function tj() {
        var cc_UUID = $("#proName option:selected").val();
        var supply_Time = $("input[name='supply_Time']").val();
        var feed_sqr = $("input[name='feed_sqr']").val();
        var f_adr = $("input[name='f_adr']").val();
        var sd = '';
        var n=0;
        var panduans=true;

        $('.addMaterial [name="fnum"]').each(function (k,v) {
			console.log($(v).attr('mat'))
            if($(v).val()==0){
                window.tool.alert('申请材料数不能小于1');
                panduans=false;
                return;
            }
			var m=$(v).attr('mat');
            //todo:2018-3-27 10:12:28去掉的
            /*if(m==1){
                n+=parseInt($(this).val());
                if(n<3&&p){
                    window.tool.alert('上覆材料类总量少于3，需支付运费');
                    panduans=false;
                    $('.addMaterial:last').after('<div class="addMaterial yunfei">'+_htmls+'</div>');
					$('.yunfei').children('.form-group').eq(0).children('.col-xs-9').children('.select-box').children('select').val(168).attr('disabled','disabled');
                    $('.yunfei').children('.form-group').eq(1).children('.col-xs-9').children('input').val(1).attr('disabled','disabled');
                    $('.yunfei').children('.form-group').eq(2).children('.col-xs-9').children('input').val('次').attr('disabled','disabled');
                    p=false;
                }else if(n>=3){
                    $('.yunfei').remove();
                    p=true;
                }
            }*/


        });
        $('.yunfei').children('.addButton').remove();
        $('.yunfei').children('.deteleButton').remove();
		


        $(".addMaterial").each(function () {
            //获取材料id
			sd += $(this).find(".select-box option:selected").val() + ',';
			//数量
            sd += $(this).find("input[name='fnum']").val() + '|';
        });
        sd = sd.substr(0,sd.lastIndexOf("|"));
        if(panduans) {
            ajaxs('api/ctteam/addMatFeed.do',
                'post',
                {
                    'cc_UUID': cc_UUID,
                    'supply_Time': supply_Time,
                    'feed_sqr': feed_sqr,
                    'f_adr': f_adr,
                    'sd': sd,
                    'p': getP()
                },
                function (data) {
                    window.tool.alert(data.msg);
                    location.href = "./material-list.html";
                }
            );
        }
    }
    $(function(){
        $(document).on('click','.addButton',function () {
            var _htmls=$(this).parent().html();
            $(this).parent().after('<div class="addMaterial">'+_htmls+'</div>');
        });
        $(document).on('click','.deteleButton',function () {
            $(this).parent().remove();
        })
    });
    $(function () {

        //判断数量输入框是否为空
        $('.form-horizontal .addMaterial').on('focus','input',function () {
            console.log($(this))
            $(this).keyup(function () {
                console.log($(this).val()<=0)
                if($(this).val()<=0){
                    $('.text-right').hide();
                    window.tool.alert('数量不能小于0,请修改');
                    return;
                }else{
                    $('.text-right').show();
                }
            })
        })


    })

</script>





